package com.mc.developmentkit.json;

import android.content.Context;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import com.mc.developmentkit.bean.AboutUsInfo;
import com.mc.developmentkit.bean.ThirdLoginInfo;
import com.mc.developmentkit.bean.TopTusInfo;
import com.mc.developmentkit.bean.UserInfo;
import com.mc.developmentkit.bean.WFTPayInfo;
import com.mc.developmentkit.bean.WXPayInfo;
import com.mc.developmentkit.bean.ZFBPayInfo;
import com.mc.developmentkit.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * Created by LeBron on 2016/12/22.
 */

public class Json {

    /**
     * @param json 微付通订单信息
     * @return
     */
    public static WFTPayInfo DNSWFTPay(String json) {
        WFTPayInfo wftPayInfo = new WFTPayInfo();
        Log.e("微付通订单信息", json);
        try {
            JSONObject jsonObject = new JSONObject(json);
            int status = jsonObject.getInt("status");
            String msg = jsonObject.getString("return_msg");
            if (status == 1) {
                wftPayInfo.tokenId = jsonObject.getString("token_id");
                //wftPayInfo.appId = jsonObject.getString("app_id");
            } else {
                Log.e("微付通支付信息", msg);
            }
            return wftPayInfo;
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("微付通订单异常：", e.toString());
            return null;
        }

    }


    /**
     * @param json 微信支付订单信息
     * @return
     */
    public static WXPayInfo DNSWXPay(String json) {
        Log.e("微信支付订单信息", json);
        WXPayInfo wxPayInfo = new WXPayInfo();
        try {
            JSONObject jsonObject = new JSONObject(json);
            int code = jsonObject.getInt("code");
            String msg = jsonObject.getString("msg");
            if (code == 0) {
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                wxPayInfo.appid = (jsonObject1.getString("appid"));
                wxPayInfo.partnerid = (jsonObject1.getString("partnerid"));
                wxPayInfo.prepayid = (jsonObject1.getString("prepayid"));
                wxPayInfo.noncestr = (jsonObject1.getString("noncestr"));
                wxPayInfo.timestamp = (jsonObject1.getString("timestamp"));
                wxPayInfo.packagvalues = (jsonObject1.getString("package"));
                wxPayInfo.sign = (jsonObject1.getString("sign"));
                return wxPayInfo;
            } else {
                Log.e("微信支付", msg);
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("微信支付异常：", e.toString());
            return null;
        }

    }

    /**
     * @param json 支付宝订单信息
     * @return
     */
    public static ZFBPayInfo DNSZFBPay(Context context,String json) {
        Log.e("支付宝订单信息", json);
        ZFBPayInfo zfbPayInfo = new ZFBPayInfo();
        try {
            JSONObject jsonObject = new JSONObject(json);
           int status=jsonObject.getInt("status");
            if (status==1){
                zfbPayInfo.orderInfo = new String(Base64.decode(jsonObject.getString("orderInfo"), Base64.DEFAULT), "utf-8");
                zfbPayInfo.outTradeNo=jsonObject.getString("out_trade_no");
                zfbPayInfo.orderSign=jsonObject.getString("order_sign");
            }else{
                Toast.makeText(context, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                Log.e("返回数据异常", "-----");
            }
            return zfbPayInfo;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("支付宝支付异常：", e.toString());
            return null;
        }

    }

    /**
     * 判断是否成功
     */
    public static boolean DNSJudge(String type, String json) {
        Log.e(type, json);
        try {
            JSONObject jsonObject = new JSONObject(json);
            int status = jsonObject.getInt("status");
            String msg = jsonObject.getString("return_msg");
            if (status == 1) {
                return true;
            } else {
                ToastUtil.showToast(msg);
                return false;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 注册、登录、个人中心信息联系模块
     * @param json
     * @return
     */
    public static AboutUsInfo aboutUs(String json) {
        Log.e("关于我们的信息", json);
        AboutUsInfo aboutUs=new AboutUsInfo();
        try {
            JSONObject jsonObject=new JSONObject(json);
            aboutUs.xyUrl=jsonObject.getString("agreement");
            JSONObject jsonObject1=jsonObject.getJSONObject("base");
            aboutUs.gNet=jsonObject1.getString("APP_NETWORK");
            aboutUs.qunNum=jsonObject1.getString("APP_QQ_GROUP");
            aboutUs.appLogo=jsonObject1.getString("APP_LOGO");
            aboutUs.jianJie=jsonObject1.getString("APP_MARK");
            aboutUs.kfPhone=jsonObject1.getString("APP_PHONE");
            aboutUs.qunKey=jsonObject1.getString("APP_QQ_GROUP_KEY");
            aboutUs.kfQQ=jsonObject1.getString("APP_QQ");
            aboutUs.swQunNum=jsonObject1.getString("BUSINESS_COOPERATION");
            aboutUs.shanPin=jsonObject1.getString("APP_SET_COVER");
            return aboutUs;
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("关于我们的信息异常：",e.toString());
            return null;
        }
    }

    /**
     * 第三方登录返回信息
     * @param loginType
     * @param json
     * @return
     */
    public static ThirdLoginInfo DNSThirdLogin(String loginType,String json) {
        Log.e("第三方登录返回的信息",json.toString());
        ThirdLoginInfo thirdLoginInfo = new ThirdLoginInfo();
        try {
            JSONObject jsonObject = new JSONObject(json);
            int code = jsonObject.getInt("code");
            String msg = jsonObject.getString("msg");
            if (code == 1) {
                JSONObject jsonObject1 = jsonObject.getJSONObject("info");
                thirdLoginInfo.icon = jsonObject1.getString("icon");
                thirdLoginInfo.nickname = jsonObject1.getString("nickname");
                if (loginType.equals("qq")) {
                    thirdLoginInfo.id = jsonObject1.getString("userID");             //QQ用户：userID
                } else if (loginType.equals("wx")) {
                    thirdLoginInfo.id = jsonObject1.getString("unionid");             //微信用户：unionid
                }
                return thirdLoginInfo;
            } else {
                ToastUtil.showToast(msg);
                return null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("获取第三方用户信息异常：",e.toString());
            return null;
        }
    }

    /**
     * 用户登录
     */
    public static UserInfo DNSLogin(String type, String json) {
        Log.e(type, json);
        UserInfo userInfo=new UserInfo();
        try {
            JSONObject jsonObject = new JSONObject(json);
            int status = jsonObject.getInt("status");
            String msg = jsonObject.getString("return_msg");
            if (status == 1) {
                userInfo.id=jsonObject.getInt("user_id");
                userInfo.login=jsonObject.getInt("type");
                userInfo.nickname=jsonObject.getString("user_account");
                userInfo.icon=jsonObject.getString("head_icon");
                userInfo.Ptb=jsonObject.getString("ptb");
                return userInfo;
            } else {
                ToastUtil.showToast(msg);
                return null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("登录解析数据异常：",e.toString());
            return null;
        }

    }


    /**
     * 轮播图
     * @param json
     * @return
     */
    public static ArrayList<TopTusInfo> DNSCarouselFigure(String json) {
        Log.e("轮播图json", json);
        ArrayList<TopTusInfo> carouselFigures = new ArrayList<TopTusInfo>();
        try {
            JSONArray jsonObject = new JSONArray(json);
            for (int i=0;i<jsonObject.length();i++){
                TopTusInfo topTu = new TopTusInfo();
                JSONObject jsonObject1 = jsonObject.getJSONObject(i);
                topTu.name=jsonObject1.getString("title");
                topTu.wang_url=jsonObject1.getString("url");
                topTu.url=jsonObject1.getString("pic_link");
                carouselFigures.add(topTu);
            }
            return carouselFigures;
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("轮播图json解析异常",e.toString());
            return null;
        }
    }


}
